package com.dayuanit.dy14.atm.demo2.dy14atmservice.service.impl;

import com.dayuanit.dy14.atm.demo2.dy14atmcommon.dto.CardDTO;
import com.dayuanit.dy14.atm.demo2.dy14atmcommon.enums.FlowTypeEnum;
import com.dayuanit.dy14.atm.demo2.dy14atmcommon.exception.BizException;
import com.dayuanit.dy14.atm.demo2.dy14atmcommon.util.CardUtils;
import com.dayuanit.dy14.atm.demo2.dy14atmcommon.util.MoneyUtil;
import com.dayuanit.dy14.atm.demo2.dy14atmdao.entity.Card;
import com.dayuanit.dy14.atm.demo2.dy14atmdao.entity.Flow;
import com.dayuanit.dy14.atm.demo2.dy14atmdao.mapper.CardMapper;
import com.dayuanit.dy14.atm.demo2.dy14atmdao.mapper.FlowMapper;
import com.dayuanit.dy14.atm.demo2.dy14atmservice.service.CardService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class CardServiceImpl implements CardService {

    @Autowired
    private CardMapper cardMapper;

    @Autowired
    private FlowMapper flowMapper;

    @Override
    public void openaccount(String password, String confirmPwd, int userId) {
        if (StringUtils.isBlank(password) || StringUtils.isBlank(confirmPwd)) {
            throw new BizException("有必填参数为空");
        }

        if (!password.equals(confirmPwd)) {
            throw new BizException("两次输入的密码不相等");
        }

        Card card = new Card();
        card.setBalance("0");
        card.setCardNum(CardUtils.createCardNum());
        card.setCreateTime(new Date());
        card.setModifyTime(card.getCreateTime());
        card.setPassword(password);
        card.setUserId(userId);
        int rows = cardMapper.insert(card);
        if (1 != rows) {
            throw new BizException("开户失败");
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deposit(int cardId, String amount, String password, int userId) {
        double amountDouble = Double.parseDouble(amount);
        if (amountDouble < 1 ) {
            throw new BizException("存款金额必须大于1元");
        }

        //悲观锁
        Card card = cardMapper.getCard4Lock(cardId);
        if (null == card) {
            throw new BizException("银行卡不存在");
        }

        if (card.getUserId() != userId) {
            throw new BizException("你无权操作");
        }

        //乐观锁
        String newBalance = MoneyUtil.plus(card.getBalance(), amount);
        int rows = cardMapper.updateBalance(cardId, card.getBalance(), newBalance);
        if (1 != rows) {
            throw new BizException("存钱失败");
        }

        // 流水
        Flow flow = new Flow();
        flow.setAmount(amount);
        flow.setCardId(cardId);
        flow.setCreateTime(new Date());
        flow.setFlowType(FlowTypeEnum.存款.getK());
        flow.setUserId(userId);
        rows = flowMapper.insert(flow);
        if (1 != rows) {
            throw new BizException("存钱失败");
        }
    }

    @Override
    public List<CardDTO> loadMyCards(int userId) {
        List<Card> cards = cardMapper.listCardByUserId(userId);
        List<CardDTO> dtos = new ArrayList<>(cards.size());

        for (Card card : cards) {
            CardDTO cardDTO = new CardDTO();
            dtos.add(cardDTO);

            cardDTO.setCardId(card.getId());
            cardDTO.setCardNum(card.getCardNum());
        }
        return dtos;
    }
}
